package com.example.delay1.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * ClassName:RabbitConfig
 * Package:com.example.delay1.config
 * Description:
 *
 * @Author:ZhangXin
 * @Create:2023/8/13-20:15
 * @Version:v1.0
 */
@Configuration
public class RabbitConfig {

    @Value("${my.exchangeName}")
    private String exchangeName;

    @Value("${my.queueNormalName}")
    private String queueNormalName;

    @Value("${my.queueDlxName}")
    private String queueDlxName;

    @Bean
    public DirectExchange directExchange() {
        return ExchangeBuilder.directExchange(exchangeName).build();
    }

    @Bean
    public Queue queueNormal() {

       return QueueBuilder
               .durable(queueNormalName)
               .ttl(25000)//队列过期时间
               .deadLetterExchange(exchangeName)//死信交换机
               .deadLetterRoutingKey("error")//死信路由key
               .build();

    }

    @Bean
    public Queue dlxQueue(){
        return QueueBuilder.durable(queueDlxName).build();
    }


    @Bean
    public Binding binding(DirectExchange directExchange, Queue queueNormal) {
        return BindingBuilder.bind(queueNormal).to(directExchange).with("order");
    }

    @Bean
    public Binding bindingDlx(DirectExchange directExchange,Queue dlxQueue){
        return BindingBuilder.bind(dlxQueue).to(directExchange).with("error");
    }

}
